package com.project.service.impl;

import com.project.bean.LogBean;
import com.project.bean.UserBean;
import com.project.mapper.ILogMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 用户接口实现类
 */
@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper mapper;

    @Autowired
    private ILogMapper logMapper;

    @Override
    public UserBean login(String account, String pwd) {
        UserBean userBean = mapper.login(account, pwd);
        if (userBean != null){
            LocalDate time = LocalDate.now();
            logMapper.add(new LogBean(userBean.getName()+"在"+time.format(DateTimeFormatter.ofPattern("HH:mm"))+"登录"));
        }
        return userBean;
    }

    @Override
    public List<UserBean> findByItem(String grade) {
        return mapper.findByItem(grade);
    }

    @Override
    public boolean add(UserBean userBean) {
        UserBean user = mapper.checkAccount(userBean.getAccount());
        if (user == null){
            mapper.add(userBean);
            return true;
        }
        return false;
    }

    @Override
    public void update(int id, String grade) {
        mapper.update(id, grade);
    }

    @Override
    public void del(int id) {
        mapper.del(id);
    }

    @Override
    public UserBean findById(int id) {
        return mapper.findById(id);
    }

    @Override
    public void updatePwd(int id, String newPwd) {
        mapper.updatePwd(id, newPwd);
    }


}
